import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Leetcode102 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        if (root==null){
            return new ArrayList<>();
        }
        LinkedList<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> list = new ArrayList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            List<Integer> tempList = new ArrayList<>();
            int size = queue.size();
            while (size>0){
                TreeNode pop = queue.pop();
                tempList.add(pop.val);
                if (pop.left!=null) queue.add(pop.left);
                if (pop.right!=null) queue.add(pop.right);
                size--;
            }
            list.add(tempList);
        }
        return list;
    }
}
